package com.sky.Screen;

import java.util.ArrayList;
import java.util.List;

import com.sky.Database.DBHelper;
import com.sky.Database.locationSQL;
import com.sky.Database.model.location;
import com.sky.Model.Item;
import com.sky.Model.LocationItem;
import com.sky.adapter.CustomGridViewAdapter;
import com.sky.adapter.listLocationAdapter;
import com.sky.heathcare.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

public class SelectLocationActivity extends Activity {

	ListView lstView;
	private DBHelper dbHelper;
	ArrayList<LocationItem> listArray = new ArrayList<LocationItem>();
	listLocationAdapter listAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sel_location_activity);
		initDB();
		initData();

		lstView = (ListView) findViewById(R.id.listLocation);
		listAdapter = new listLocationAdapter(this, R.layout.location_item,
				listArray);
		lstView.setAdapter(listAdapter);

		lstView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				ImageView map = (ImageView) findViewById(R.id.mapImg);
				LocationItem item = listArray.get(position);
				map.setImageBitmap(item.getImage());

			}
		});

		Button selectBtn = (Button) findViewById(R.id.Select_btn);
		selectBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Check the login
				// Save in client

				// Save location in client for next session
				// Change select location
				gotoMainScreen();

			}
		});

	}

	private void initDB() {
		this.dbHelper = new DBHelper(this);
	}
	
	private void initData() {
		
		locationSQL officeSQL = new locationSQL(dbHelper);
		
		List<location> lst =  officeSQL.getOfficeList();
		Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.coso1);
		Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.coso1);

		listArray.add(new LocationItem(homeIcon, "Co so 1:", "0968756523"));
		listArray.add(new LocationItem(userIcon, "Co so 2", "0968756523"));
		listArray.add(new LocationItem(homeIcon, "Co so 3", "0968756523"));
		listArray.add(new LocationItem(userIcon, "Co so 4", "0968756523"));
		listArray.add(new LocationItem(homeIcon, "Co so 5", "0968756523"));
		listArray.add(new LocationItem(userIcon, "Co so 6", "0968756523"));

	}
	
	@Override
	protected void onStop() {
		if (dbHelper != null)
			dbHelper.close();
		super.onStop();
	}
	private void gotoMainScreen() {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("USER_STATUS", true);
		startActivity(intent);
		finish();
	}
}
